package TMS.src;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class FavList implements Serializable {
    static class favCommodity extends Commodity implements Serializable {
        public favCommodity(String shopId, String commodityId, Integer num, Double price) {
            this.setCommodityId(commodityId);
            this.setShopId(shopId);
            this.setCommodityNum(num);
            this.setCommodityPrice(price);
        }
    }

    User master;
    List<favCommodity> favCommodities = new ArrayList<>();

    public FavList() {
        this.master = Const.current_user;
    }

    public FavList(String shopId, String commodityId, Integer num, Double price) {
        this.master = Const.current_user;
        favCommodities.add(new favCommodity(shopId, commodityId, num, price));
    }

    public boolean isEmpty() {
        return favCommodities.isEmpty();
    }
    public List<favCommodity> getCommodities() {
        return favCommodities;
    }
    public void addCommodity(String shopId, String commodityId, Integer num, Double price) {
        for (favCommodity c : favCommodities) {
            if (c.getCommodityId().equals(commodityId) && c.getShopId().equals(shopId)) {
                c.setCommodityNum(c.getCommodityNum() + num);
                return;
            }
        }
        favCommodities.add(new favCommodity(shopId, commodityId, num, price));
    }

    public boolean deleteCommodity(String shopId, String commodityId) {
        return favCommodities.removeIf(c -> c.getCommodityId().equals(commodityId) && c.getShopId().equals(shopId));
        /*for(favCommodity c:favCommodities){
            if(c.getCommodityId().equals(commodityId)&& c.getShopId().equals(shopId)){
                favCommodities.remove(c);
            }
        }*/
    }

    public void listCommodities() {
        for (favCommodity c : favCommodities) {
            System.out.println(c.getShopId() + ": " + c.getCommodityId() + " " + c.getCommodityNum() + " " + String.format("%.2f", c.getCommodityPrice()) + "yuan");
        }
    }
    public void buyCommodities() {
        Commodity co = new Commodity();
        for(favCommodity c : favCommodities){
            String[] cmd={"buyCommodity",c.getShopId(),c.getCommodityId(), String.valueOf(c.getCommodityNum())};
            String ret=co.BuyCommodity(cmd);
            if(ret.contains("success")){//购买成功,输出订单信息
                int stIndex = ret.indexOf('O');
                int endIndex = ret.indexOf(')');
                String orderId=ret.substring(stIndex, endIndex);
                Order order = Const.allOrder_existed.get(orderId);
                String status = new String[]{"pending", "canceled", "finished"}[order.getOrderStatus()];
                String out = order.getOrderId() + ": " + order.getCommodity().getShopId() + " " + order.getCommodity().getCommodityId() + " " + order.getQuantity() + " " + String.format("%.2f", order.getCommodity().getCommodityPrice() * order.getQuantity()) + "yuan " + status;
                System.out.println(out);
            }else
                System.out.println(ret);
        }
        favCommodities.clear();
    }
}
